<?php
/**
 * 
 * @authors 丛玉坤(you@example.org)
 * @date    2016-06-25 22:00:22
 * @version 一版
 */
namespace Home\Controller;
use Think\Controller;
class BaodanController extends Controller {

		public function _initialize(){
      	header("content-type:text/html;charset=utf-8");
      	
      }
	public function index()
	{    
		$user=$_SESSION['id'];
		//判断用户登录
		if ($_SESSION['id']!='') { 
			// if($user!=''){	
				$Bao=M("Baodan");
				//加入保单详情join("in_plan on in_baodan.jid= in_plan.jid")->预留的保单信息如若 有时间限制是启用
				$wheres['uid']=$user;
				// $wheres['status']=1;
				$baodan=$Bao->where($wheres)->join("in_photos on in_baodan.jid=in_photos.jid")->order("status DESC")->select();
				$this->baodan=$baodan;	   
		   	$this->display();
		   }else{

		   	    echo "<script>
		   	            location.href='/Index/index';//客户端跳转
		   	          </script>";

		   } 
	} 
	public function baoDanInfo()
	{
		 $b=I("get.bao");
		// die;
		if($b!='') {
		$Bao=M("Baodan");
		$baodan=$Bao->where('bid='.$b)->join("in_photos on in_baodan.jid=in_photos.jid")->join("in_plan on in_baodan.jid= in_plan.jid")->find();
		$this->baodan=$baodan;
		//判断是否申请公示
		
		$Binfo=M("Baodan")->where('bid='.$baodan['bid'])->find();
		$User=M("Qinqing");
		// $QArr=$User->where('id='.$info['qid'])->select();
		$Uarr=M("User")->where('id='.$_SESSION['id'])->find();
				$wheres['sname']=$Binfo['jname'];
				$wheres['uid']=$_SESSION['id'];
				$wheres['fcardid']=$Uarr['userid'];
				$wheres['fuser']=$Uarr['realname'];
				$wheres['jid']=$Binfo['jid'];
				$wheres['buser']=$Binfo['username'];
				$wheres['bcardid']=$Binfo['userid'];
				$Shen=M("Shenhe");
				$unique=$Shen->where($wheres)->find();

		 $tj=M("Shenhe")->where($wheres)->find();         
		    
		//判断等待期
		  $end=strtotime($baodan['usetime'])-time();
		  if($end>0){
			$this->dengdai=2;//未到等待期
		  }else{
		  	$this->dengdai=1;//过等待期
		  }
		  //会员公约
		   $other=M("Other")->where('id=1')->find();
			$this->gongyue=$other['gongyue'];
 			$this->liucheng=$other['liucheng'];

 			$kuan=M("Content")->field('content')->where('jid='.$baodan['jid'])->find();
 			$this->kuan=$kuan;
 			   
		$this->tiao=$tj; 
		} 
		 $this->display();
	}
	public function exitbao()
	{
		$qian=M("Qianbao");//钱包
		$P=M("Plan");//计划
		$jl=M("Jilu");//记录

		 $bid=I("post.bao"); 
		 $Bao=M("Baodan");
		 $BArr=$Bao->field("uid,jid,yufujin")->where('bid='.$bid)->find();
		   $tuichu=$P->field("status")->where("jid=".$BArr['jid'])->find(); 
		 if ($tuichu['status']=='4') {
		 	echo 0; //已经进入筹款不能退出
		 	die;
		 }
  
		 $k=$Bao->where("bid=".$bid)->find();	    
		 if ($k['status']==0) {
		 	echo 3;die;
		 }
		 $B['status']=0;
		 $B['yufujin']=0;
		 $Baodan=$Bao->where("bid=".$bid)->save($B);
		 // 核算退款钱包总数
		 $wheres['uid']=$BArr['uid'];
		$qm= $qian->where($wheres)->find();
	    $data['money']=$qm['money']+$BArr['yufujin'];
	    //修改总金额
	    $addqian=$qian->where($wheres)->save($data);
	    //写入记录
	    $J['uid']=$BArr['uid'];
	    $J['jine']=$BArr['yufujin'];
	    $j['liuxiang']=9;
	    $J['yue']=$data['money'];
	    $J['jiao_type']=0;
	    $J['create_time']=date("Y-m-d H:i:s",time());
	    // 进行记录的写入
	    $Ji=$jl->add($J);
	   //扣除计划中的金额
	   $Parr=$P->field("allmoney")->where("jid=".$BArr['jid'])->find();

	   $tiao['allmoney']=$Parr['allmoney']-$BArr['yufujin'];

	   $jihua=M("Plan")->where("jid=".$BArr['jid'])->save($tiao);
	   // echo $P->getlastsql();
		if ($jihua&&$addqian&&$Ji&&$Baodan) {
			echo '1';
		}else{
			echo '2';
		}
		      
		// $this->display();
	}

	//保单充值
	public function chongzhi()
	{
		$bao=I('get.bao');
		$this->bid=$bao;

		if (IS_POST) {
		   $id=I("post.user");
		   $money=I("post.nums");
		   if ($id!='') {
		   	$Qian=M("Qianbao");
		   	$Qarr=$Qian->where('uid='.$id)->find();

		   	if ($Qarr['zfpassword']=='') {
		   	  	echo "0";//没有密码
		   	  	die;
		   	  }else{
		   	  	if ($Qarr['money']<$money) {
		   	 	echo "2";//余额不足
		   	  	die;
		   	  }else
		   	   {
		   	   	echo '3';
		   	  }
		   	  }
		   }
		}else{
			$this->display();
		}
	}

public function yanzheng()
{
      $bid=I("post.h");
	  $pass=md5(I("post.p"));
	  $uid=I("post.user");
      $money=I("post.nums");

	 if ($uid!='') {
	 	$Qian=M("Qianbao");
		$Qarr=$Qian->where('uid='.$uid)->find();
		if ($Qarr['zfpassword']==$pass) {
			 $data['money']=$Qarr['money']-$money;
			$kou=$Qian->where('uid='.$uid)->save($data);
			  if ($kou) {
		   	  	$da['uid']=$uid;
		   	  	$da['yue']=$data['money'];
		   	  	$da['create_time']=date("Y-m-d H:i:s",time());
		   	  	$da['liuxiang']=10;
		   	  	$da['jine']=$money;
		   	  	$J=M('Jilu');
		   	  	$j=$J->add($da);
		   	  	if ($j) { 
		   	  		$Bao=M("Baodan");
		   	  		$Barr=$Bao->field('yufujin')->where('bid='.$bid)->find();
		   	  		$ba['yufujin']=$Barr['yufujin']+ $money;
		   	  		$b=$Bao->where('bid='.$bid)->save($ba);
		   	  		if ($b) {
		   	  			echo "<script>
		   	  		            alert('成功');
		   	  		            history.go(-3);
		   	  		          </script>";
		   	  		 }	    
		   	  	}
		   	  }else{
		   	  echo	$Qian->getError();}
		}else{
			echo "<script>
		   	  	   alert('密码不正确');
		   	  	   history.go(-1);
		   	  	   </script>";
		}
	 }

}
	//互保公示
	public function addhubao()
	{
		$this->type=$type=I("get.type");
		$gid=I("get.gid")?$gid=I("get.gid"):$gid=I("post.gid");
		$this->gid=$gid;
		$Binfo=M("Baodan")->where('bid='.$gid)->find();
		$User=M("Qinqing");
		// $QArr=$User->where('id='.$info['qid'])->select();
		$Uarr=M("User")->where('id='.$_SESSION['id'])->find();
		if ($Binfo['qid']==0) {
			$this->user=$Uarr['realname'];
			$this->with='本人';
		}else{
			$QArr=$User->where('id='.$Binfo['qid'])->find();
			$this->user=$QArr['qname'];
			$this->with=$QArr['twith'];

		}

		if (IS_POST) {
				 $upload = new \Think\Upload();// 实例化上传类
				 $upload->saveName = time().'_'.mt_rand();
				 $upload->maxSize   =     3145728 ;// 设置附件上传大小
				 $upload->exts      =     array("docx");// 设置附件上传类型
				 $upload->savePath  =      '/'; // 设置附件上传目录// 上传文件 
				 $info   =   $upload->upload();if(!$info) {// 上传错误提示错误信息    
				 $this->error($upload->getError());
				}else{
				// 上传成功 获取上传文件信息    
				foreach($info as $file){        
					 $data['Wsrc']= 'Uploads'.$file['savepath'].$file['savename'];    
				}} 
				$data['bid']=$Binfo['bid'];
				$data['sname']=$Binfo['jname'];
				$data['fcardid']=$Uarr['userid'];
				$data['fuser']=$Uarr['realname'];
				$data['buser']=$Binfo['username'];
				$data['jid']=$Binfo['jid'];
				$data['bcardid']=$Binfo['userid'];
				$data['uid']=$_SESSION['id'];
				$data['miaoshu']=I("post.content");
				$data['createtime']=date("Y-m-d H:i:s");

				$wheres['bid']=$Binfo['bid'];
				$wheres['sname']=$Binfo['jname'];
				$wheres['uid']=$_SESSION['id'];
				$wheres['fcardid']=$data['fcardid'];
				$wheres['fuser']=$Uarr['realname'];
				$wheres['jid']=$Binfo['jid'];
				$wheres['buser']=$data['buser'];
				$wheres['bcardid']=$Binfo['userid'];
				$Shen=M("Shenhe");
				$unique=$Shen->where($wheres)->find();
				if ($unique!='') {
					    echo "<script>
					            alert('申请过了不要重复申请');
					            history.go(-3);
					          </script>";
				}else{

					$shen=$Shen->add($data);
					if ($shen) {
						    
						    echo "<script>
						            alert('申请成功！请耐心等待');
						            history.go(-2);
						          </script>";
					}
				}			    

		}else{		   
		$this->display();
		}
	}
	//公示详情
	public function gonginfo()
	{
		$Shen=M("Shenhe");

		$this->display();
	}
	//保单详情
	public function application()
	{
 
		  $bid=I("get.xq");
		 $Shen=M("Shenhe");
		 $xiang=$Shen->where('sid='.$bid)->find();
		   $Bao=M("Baodan");
		   $time=$Bao->field('addtime')->where("bid=".$xiang['bid'])->find();
		   $this->time=$time;
		 $this->xiang=$xiang;

		$this->display();
	}

}